// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.jobs.joinorder.hypergraph;

import org.apache.doris.nereids.jobs.joinorder.hypergraph.bitmap.LongBitmap;
import org.apache.doris.nereids.jobs.joinorder.hypergraph.bitmap.LongBitmapSubsetIterator;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashSet;

public class BitmapTest {
    @Test
    void subsetIteratorTest() {
        int[] ints = {1, 3, 5, 12, 21, 32, 43, 54, 60, 63};
        long bitmap = LongBitmap.newBitmap(ints);
        LongBitmapSubsetIterator subsetIterator = new LongBitmapSubsetIterator(bitmap);
        HashSet<Long> subsets = new HashSet<>();
        for (long subset : subsetIterator) {
            Assertions.assertTrue(LongBitmap.isSubset(subset, bitmap));
            subsets.add(subset);
        }
        Assertions.assertEquals(subsets.size(), Math.pow(2, ints.length) - 1);
        subsetIterator.reset();
        subsets.clear();
        for (long subset : subsetIterator) {
            Assertions.assertTrue(LongBitmap.isSubset(subset, bitmap));
            subsets.add(subset);
        }
        Assertions.assertEquals(subsets.size(), Math.pow(2, ints.length) - 1);
    }

    @Test
    void iteratorTest() {
        int[] ints = {1, 3, 6, 12, 43};
        long bitmap = LongBitmap.newBitmap(ints);
        int index = 0;
        for (int v : LongBitmap.getIterator(bitmap)) {
            Assertions.assertEquals(ints[index], v);
            index += 1;
        }
        index = 0;
        for (int v : LongBitmap.getReverseIterator(bitmap)) {
            Assertions.assertEquals(ints[ints.length - 1 - index], v);
            index += 1;
        }
    }
}
